package com.hlm.demo.controller.account;

import com.hlm.demo.common.ResultWrapper;
import com.hlm.demo.entity.Account;
import com.hlm.demo.service.account.IAccountService;
import com.hlm.demo.validator.accountvalidator.AccountAddValidator;
import com.hlm.demo.validator.accountvalidator.AccountUpdValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author MRD
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private IAccountService accountService;

    /**
     * 查询出所有的账单集合
     *
     * @param page
     * @param count
     * @return
     */
    @RequestMapping("/findAllAccount")
    @ResponseBody
    public ResultWrapper findAllAccount(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "3") Integer count,
                                        String idSearch, String nameSearch,
                                        String usernameSearch, String stateSearch
    ) {

        List<Account> allAccount = accountService.findAllAccount(page, count, idSearch, nameSearch, usernameSearch, stateSearch);

        Integer totalCount = accountService.totalCount(idSearch,nameSearch,usernameSearch,stateSearch);
        return ResultWrapper.success(totalCount, allAccount);
    }

    /**
     * 根据Id查找一个account对象
     *
     * @param accountId
     * @return
     */
    @RequestMapping("/findAccountById")
    @ResponseBody
    public ResultWrapper findAccountById(Integer accountId) {

        Account account = accountService.findAccountById(accountId);
        return ResultWrapper.success(account);
    }

    /**
     * 更新一个账务账单的信息
     *
     * @param account
     * @return
     */
    @RequestMapping("/updAccount")
    public String updAccount(@Validated(value = {AccountUpdValidator.class}) Account account
            , BindingResult result) {
        if (result.hasErrors()) {
            List<FieldError> errors = result.getFieldErrors();
            errors.forEach(System.out::println);
        } else {
            accountService.updAccount(account);
        }
        return "redirect:/account/account_modi.html";
    }

    /**
     * 更改某一个账务账单的状态
     *
     * @param accountId
     * @param status
     * @return
     */
    @RequestMapping("/updStatus")
    public String updStatus(Integer accountId, String status) {
        System.out.println("该状态啊啊啊啊" + accountId + "状态改成啥啊" + status);
        accountService.updStatus(accountId, status);
        return "redirect:/account/account_list.html";
    }

    /**
     * 添加一条信息
     */
    @RequestMapping("/addAccount")
    public String addAccount(@Validated(value = {AccountAddValidator.class}) Account account
            , BindingResult result, String birthdate) {
        if (result.hasErrors()) {
            List<FieldError> errors = result.getFieldErrors();
            errors.forEach(System.out::println);
            return "redirect:/account/account_add.html";
        } else {
            System.out.println("我要添加一条账务账单的信息:::" + account);
            accountService.addAccount(account);

            return "redirect:/account/account_list.html";
        }
    }


}
